﻿using System;
using System.Collections;
using System.Collections.Generic;
using Component;
using Component.UI;
using UnityEditor;
using UnityEngine;


public class DrawLineTools : DrawMeshTools
{
    public Color Color { get; set; }
    private bool waitToInvisible;
    private StationNode nextNode;
    private StationNode lastNode;

    private List<Transform> trainList;

    public StationNode GetNode(StationNode node)
    {
        if (node == lastNode)
        {
            return nextNode;
        }
        if (node == nextNode)
        {
            return lastNode;
        }

        return null;
    }

    public StationNode GetNode()
    {
        return lastNode;
    }

    public void ClearNode()
    {
        nextNode = null;
        lastNode = null;
    }

    public void ReplaceNode(StationNode original, StationNode node)
    {
        if (nextNode == original)
        {
            nextNode = node;
        }

        if (lastNode == original)
        {
            lastNode = node;
        }
    }
    public void SetNode(StationNode node)
    {
        if (lastNode == null)
        {
            lastNode = node;
        }else if (nextNode == null)
        {
            nextNode = node;
        }
    }

    public bool NextNodeIsNull(StationNode node)
    {
        if (node == lastNode)
        {
            return nextNode == null;
        }

        if (node == nextNode)
        {
            return lastNode == null;
        }

        return true;
    }
    
    public Vector2 StartPosDir()
    {
        return vertices[vertices.Length - 1] - vertices[vertices.Length - 3];
    }
    public Vector2 EndPosDir()
    {
        return vertices[0] - vertices[2];
    }

    public void CopyLine(DrawLineTools anther)
    {
        lineData = anther.LineData;
        nextNode = anther.nextNode;
        lastNode = anther.lastNode;
    }

    public void DrawEndNode(Vector2 direction, Vector2 original, float width, float height, Color color)
    {
        Vector3 o = original;

        Vector3 horizontal = Vector3.right;
        Vector3 vertical = Vector3.up;
        
        if (direction.x < 0)
        {
            horizontal = -horizontal;
            vertical = -vertical;
        }

        if (direction.y < 0)
        {
            vertical = -Vector3.right;
            horizontal = -Vector3.up;
        }else if (direction.y > 0)
        {
            vertical = Vector3.right;
            horizontal = Vector3.up;
        }

        vertices = new[]
        {
            o - vertical * width / 2, o + vertical * width / 2,
            o - vertical * width / 2 + horizontal * height, o + vertical * width / 2 + horizontal * height,
            o - vertical * width + horizontal * height, o + vertical * width + horizontal * height,
            o - vertical * width + horizontal * height /4 * 5, o + vertical * width + horizontal * height/4*5,
        };

        triangles = new[]
        {
            0, 1, 3,
            3, 2, 0,
            4, 5, 6,
            5, 7, 6
        };
        if (Math.Abs(direction.y) > Mathf.Epsilon)
        {
            Array.Reverse(triangles);
        }
        CreateMesh(color);
    }

    protected override void CreateMesh(Color color)
    {
        base.CreateMesh(color);
        Color = color;
    }

    private Task visibleTask;
    /// <summary>
    /// 当前线需要消失，但其上还有火车，需要等火车开过才能消失
    /// </summary>
    /// <param name="train">其上的火车</param>
    public void LineInvisibleTask(Transform train)
    {
        if (trainList == null)
        {
            trainList = new List<Transform>();
        }
        trainList.Add(train);
    }
    
    /// <summary>
    /// 移除开过的火车
    /// </summary>
    /// <param name="train"></param>
    public void TrainRunWay(Transform train)
    {
        trainList.Remove(train);
    }

    public IEnumerator ResetLine()
    {
        if (trainList == null)
        {
            print("list is null");
            ClearNode();
            ClearMesh();
            yield break;
        }
        for (int i = 0; i < Colors.Length; i++)
        {
            Colors[i] = new Color(Colors[i].r, Colors[i].g, Colors[i].b, .5f);
        }

        try
        {
            mesh.colors = Colors;
        }
        catch (Exception e)
        {
            throw;
        }
        
        waitToInvisible = true;
        while (trainList.Count > 0)
        {
            yield return new WaitForSeconds(.2f);
        }

        waitToInvisible = false;
        ClearNode();
        ClearMesh();
    }
    
}
